'''
= Flatten Collections =

This method will return the collection with all sub-collections
flattened. Taken from:
<http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html>
'''

import types


def flattened(collection, inner_types = (list, tuple)):
    outer_type = type(collection)
    collection = list(collection)
    
    if outer_type == types.GeneratorType:
        outer_type = tuple
    
    i = 0
    
    while i < len(collection):
        while isinstance(collection[i], inner_types):
            if not collection[i]:
                collection.pop(i)
                i -= 1
                break
            else:
                collection[i:i + 1] = collection[i]
        i += 1
    
    return outer_type(collection)
